let express = require('express');
let router = express.Router();

const studentService = require('../service/studentService');
const checkJWTMiddleWare = require('../middleWare/checkJWTMiddleWare');

// 此路由需要登录后才能访问
router.use(checkJWTMiddleWare);

/*
 * 分页获取学生列表接口
 */
router.post('/studentList', async function (req, res, next) {
    let pg = req.body.pg;
    if (!pg) {
        pg = 1;
    }
    let result = await studentService.getStudentList(req.jwtResult.data.uid, req.jwtResult.data.userType, pg);
    res.status(result.success ? 200 : 400).json(result);
});

// /*
//  * 获取所有学生列表的接口
//  */
// router.post('/studentList', async function (req, res, next) {
//     let result = await studentService.getStudentList(req.jwtResult.data.uid, req.jwtResult.data.userType);
//     res.status(result.success ? 200 : 400).json(result);
// });

/*
 * 查询学生详细信息接口
 */
router.post('/studentInfo', async function (req, res, next) {
    let studentId = req.body.studentId;
    if (!studentId) {
        res.status(400).json({ success: false, msg: "您疑似爬虫，已记录到系统日志" });
        return ;
    }
    let result = await studentService.getStudentInfo(req.jwtResult.data.uid, req.jwtResult.data.userType, studentId);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 获取所有学生列表接口
 */
router.get('/getAllStudentList', async function (req, res, next) {
    let result = await studentService.getAllStudentListLimit(req.jwtResult.data.uid, req.jwtResult.data.userType);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 搜索学生列表接口
 */
router.post('/searchStudentList', async function (req, res, next) {
    let name = req.body.name;
    let cardId = req.body.cardId;
    let className = req.body.className;
    let facultyId = req.body.facultyId;
    let tname = req.body.tname;
    let warningLevelId = req.body.warningLevelId;
    let result = await studentService.searchStudentListLimit(req.jwtResult.data.uid, req.jwtResult.data.userType, name, cardId, className, facultyId, tname, warningLevelId);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 新增学生基本信息接口
 */
router.post('/addStudentInfo', async function (req, res, next) {
    let cardId = req.body.cardId;
    let name = req.body.name;
    let className = req.body.className;
    let sex = req.body.sex;
    let phone = req.body.phone;
    let birthday = req.body.birthday || null;
    let socialPlatform = req.body.socialPlatform || '';
    let address = req.body.address || '';
    let fatherOccupation = req.body.fatherOccupation || '';
    let motherOccupation = req.body.motherOccupation || '';
    let familyEcon = req.body.familyEcon || '';
    let familyStruct = req.body.familyStruct || '';
    let isOnlyChild = req.body.isOnlyChild || 'false';
    let hasFamilyHistoryIll = req.body.hasFamilyHistoryIll || 'false';
    let academic = req.body.academic || '在读';
    if (!cardId || !name || !className || !sex || !phone) {
        res.status(400).json({ success: false, msg: "参数不全，无法修改" });
        return ;
    }
    let data = { cardId, name, className, sex, phone, birthday, socialPlatform, address, fatherOccupation, motherOccupation, familyEcon, familyStruct, isOnlyChild, hasFamilyHistoryIll, academic };
    let result = await studentService.addStudentInfo(req.jwtResult.data.uid, req.jwtResult.data.userType, data);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 修改学生基本信息接口
 */
router.post('/updateStudentInfo', async function (req, res, next) {
    let studentId = req.body.studentId;
    let cardId = req.body.cardId;
    let name = req.body.name;
    let classId = req.body.classId;
    let sex = req.body.sex;
    let phone = req.body.phone;
    let birthday = req.body.birthday || null;
    let socialPlatform = req.body.socialPlatform || '';
    let address = req.body.address || '';
    let fatherOccupation = req.body.fatherOccupation || '';
    let motherOccupation = req.body.motherOccupation || '';
    let familyEcon = req.body.familyEcon || '';
    let familyStruct = req.body.familyStruct || '';
    let isOnlyChild = req.body.isOnlyChild || 'false';
    let hasFamilyHistoryIll = req.body.hasFamilyHistoryIll || 'false';
    let academic = req.body.academic || '在读';
    if (!studentId || !cardId || !name || !classId || !sex || !phone) {
        res.status(400).json({ success: false, msg: "参数不全，无法修改" });
        return ;
    }
    let data = { studentId, cardId, name, classId, sex, phone, birthday, socialPlatform, address, fatherOccupation, motherOccupation, familyEcon, familyStruct, isOnlyChild, hasFamilyHistoryIll, academic };
    let result = await studentService.updateStudentInfo(req.jwtResult.data.uid, req.jwtResult.data.userType, data);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 修改学生预警级别接口
 */
router.post('/updateStudentWarningLevel', async function (req, res, next) {
    let studentId = req.body.studentId;
    let warningLevelId = req.body.warningLevelId;
    if (!studentId || !warningLevelId) {
        res.status(400).json({ success: false, msg: "参数不全，无法修改" });
        return ;
    }
    let data = { studentId, warningLevelId };
    let result = await studentService.updateStudentWarningLevel(req.jwtResult.data.uid, req.jwtResult.data.userType, data);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 通过 学号 姓名 查询学生的ID值
 */
router.post('/findStudentIdByNameAndCardId', async function (req, res, next) {
    let cardId = req.body.cardId;
    let name = req.body.name;
    if (!cardId || !name) {
        res.status(400).json({ success: false, msg: "参数不全，无法查询" });
        return ;
    }
    let result = await studentService.findStudentIdByNameAndCardId(cardId, name);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 获取所有班级接口
 */
router.get('/getAllClassList', async function (req, res, next) {
    let result = await studentService.getAllClassList();
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 获取所有学院接口
 */
router.get('/getAllFacultys', async function (req, res, next) {
    let result = await studentService.getAllFacultys();
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 查看所有预警级别接口
 */
router.get('/getAllWarningLevelList', async function (req, res, next) {
    let result = await studentService.getAllWarningLevelList();
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 导出学生信息的接口
 */
router.post('/exportStudentInfo', async function (req, res, next) {
    console.log('【导出学生信息 请求到达的时间】', Date.now());
    let studentIdList = req.body.studentIdList;
    if (!(Array.isArray(studentIdList))) {
        res.status(400).json({ success: false, msg: "studentIdList参数必须是一个数组" });
        return ;
    }
    if (studentIdList.length == 0) {
        res.status(400).json({ success: false, msg: "学生ID列表为空" });
        return ;
    }
    if (studentIdList.length > 1000) {
        res.status(400).json({ success: false, msg: "导出数据过多" });
        return ;
    }
    let result = await studentService.exportStudentInfo(req.jwtResult.data.uid, req.jwtResult.data.userType, studentIdList);
    console.log('【导出学生信息 开始响应请求的时间】', Date.now());
    res.status(result.success ? 200 : 400).json(result);
    console.log('【导出学生信息 响应完毕的时间】', Date.now());
});

module.exports = router;